package org.tp8.sampleapp.security.model;

import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlID;
import javax.xml.bind.annotation.XmlIDREF;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import org.springframework.security.core.GrantedAuthority;
import org.tp8.sampleapp.jaxb.adapter.XmlLongAdapter;

/**
 * The Class Role.
 */
@Entity
@Table(name = "role")
@XmlRootElement(name = "role")
@XmlAccessorType(XmlAccessType.FIELD)
public class Role implements BaseAuthModel, GrantedAuthority {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 7874094534658077866L;

	/** The id. */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "id")
	@XmlElement(name = "id")
	@XmlID
	@XmlJavaTypeAdapter(XmlLongAdapter.class)
	private Long id;

	/** The name. */
	@Column(name = "name")
	@XmlElement(name = "name")
	private String name;

	/** The description. */
	@Column(name = "description")
	@XmlElement(name = "description")
	private String description;

	/** The users. */
	@ManyToMany(mappedBy = "authorities", fetch = FetchType.LAZY)
	@XmlElementWrapper(name = "users")
	@XmlElement(name = "user")
	@XmlIDREF
	private Collection<User> users;

	/**
	 * Gets the users.
	 * 
	 * @return the users
	 */
	public Collection<User> getUsers() {
		return users;
	}

	/**
	 * Sets the users.
	 * 
	 * @param users
	 *            the users to set
	 */
	public void setUsers(Collection<User> users) {
		this.users = users;
	}

	/**
	 * Gets the name.
	 * 
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * Sets the name.
	 * 
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * Gets the id.
	 * 
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * Sets the id.
	 * 
	 * @param id
	 *            the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * Gets the description.
	 * 
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * Sets the description.
	 * 
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.security.core.GrantedAuthority#getAuthority()
	 */
	@Override
	public String getAuthority() {
		return name;
	}

}
